package com.ruoyi.system.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.PoPublicOpinion;
import com.ruoyi.system.domain.PublicOpinionEvents;
import com.ruoyi.system.domain.PublicOpinionTopic;
import com.ruoyi.system.domain.vo.NameListValue;
import com.ruoyi.system.domain.vo.NameValue;
import com.ruoyi.system.mapper.PoPublicOpinionMapper;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class StatisticAnalysisServiceImpl implements IStatisticAnalysisService {
    @Autowired
    PoPublicOpinionMapper publicOpinionMapper;
    @Autowired
    ElasticsearchBasicService elasticsearchBasicService;
    @Autowired
    IPublicOpinionTopicService publicOpinionTopicService;
    @Autowired
    IPublicOpinionEventsService publicOpinionEventsService;

    /**
     * 今日预警总数
     *
     * @param tenantCode
     * @return
     */
    @Override
    public Integer getTodayAlertCount(String tenantCode) {
        List<String> ids = publicOpinionMapper.queryWarningIds(null, tenantCode);
        return elasticsearchBasicService.getTodayAlertCount(ids);
    }

    /**
     * 今日负面总数
     * @param tenantCode
     * @return
     */
    @Override
    public Integer getTodayNegativeCount(String tenantCode) {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return 0;
        }
        return elasticsearchBasicService.getTodayNegativeCount(topics,events);
    }

    @Override
    public Integer getTodayPublicOpinionCount(String tenantCode) {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return 0;
        }
        return elasticsearchBasicService.getTodayPublicOpinionCount(topics,events);
    }

    @Override
    public Integer getPastSevenDaysPublicOpinionCount(String tenantCode) {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return 0;
        }
        return elasticsearchBasicService.getPastSevenDaysPublicOpinionCount(topics,events);
    }

    /**
     * 近七日负面信息
     * @return
     */
    @Override
    public List<NameListValue> negativeInformationHeatMap() {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return new ArrayList<>();
        }
        return elasticsearchBasicService.negativeInformationHeatMap(topics,events);
    }

    @Override
    public List<NameListValue> getSentimentByPublishTimeByTenantCode() {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return new ArrayList<>();
        }
        return elasticsearchBasicService.getSentimentByPublishTimeByTenantCode(topics,events);
    }

    @Override
    public List<NameValue> getPublicOpinionCountGroupByProvinceOfTenantCode() {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return new ArrayList<>();
        }
        return elasticsearchBasicService.getPublicOpinionCountGroupByProvinceOfTenantCode(topics,events);
    }

    @Override
    public List<NameValue> getHotWords() {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return new ArrayList<>();
        }
        return elasticsearchBasicService.getHotWords(topics,events);
    }

    @Override
    public List<NameValue> getMediaActivity() {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return new ArrayList<>();
        }
        return elasticsearchBasicService.getMediaActivity(topics,events);
    }

    @Override
    public List<NameValue> getOpinionLeaders() {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return new ArrayList<>();
        }
        return elasticsearchBasicService.getOpinionLeaders(topics,events);
    }

    @Override
    public List<PoPublicOpinion> getNegativeListOfTenantCode(int count) {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return new ArrayList<>();
        }
        return elasticsearchBasicService.getNegativeListOfTenantCode(topics,events,count);
    }

    @Override
    public List<PoPublicOpinion> getHotListOfTenantCode(int count) {
        PublicOpinionTopic topic=new PublicOpinionTopic();
        topic.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionTopic> topics = publicOpinionTopicService.selectPublicOpinionTopicList(topic);
        PublicOpinionEvents event=new PublicOpinionEvents();
        event.setTenantCode(SecurityUtils.getLoginUser().getUser().getTenantCode());
        List<PublicOpinionEvents> events = publicOpinionEventsService.selectPublicOpinionEventsList(event);
        if(topics.size()==0&&events.size()==0){
            return new ArrayList<>();
        }
        return elasticsearchBasicService.getHotListOfTenantCode(topics,events,count);
    }
}
